
        $(document).ready(function () {

            //购物车
            $("#top_right_menu").stop().slideUp(0);
            $("#top_right").mouseover(function () {

                $("#top_right_menu").stop().slideDown(150);
            })
            $("#top_right").mouseout(function () {

                $("#top_right_menu").stop().slideUp(300);
            })

            //横向导航条          
            $("#j_navMenu").stop().slideUp(0);

            $(".text").on('mouseenter', function () {
                $("#j_navMenu").stop().slideDown(150);
                $(this).closest("li").addClass("add").siblings().removeClass("add"); //siblings:查询所有兄弟节点  closest:向上查找指定元素            
                $("#j_navMenu").empty().append($(".add .container").clone());

            })

            $("#headerid").on('mouseleave', function () {

                $("#j_navMenu").stop().slideUp(100);

            })

            $(".text:gt(7)").off().on('mouseenter', function () {

                $("#j_navMenu").stop().slideUp(100);

            })

            $(".header_logo,.header_search").on('mouseover', function () {
                $("#j_navMenu").stop().slideUp(100);
            })



            //搜索框

            $(".search-btn").on('mouseover', function () {
                $(".search-text").addClass("bordercolor");
            })
            $(".search-btn").on('mouseout', function () {
                $(".search-text").removeClass("bordercolor");
            })

            $(".keyword-list").on('mouseenter', 'li', function () {

                $(this).addClass("backg").siblings().removeClass("backg");
            })
            $(".keyword-list").on('mouseleave', function () {
                $(".keyword-list li").removeClass("backg");
            })



            //竖向导航菜单
            var sub = $('#sub');
            var activeRow; //一级菜单
            var activeMenu;
            var timer;
            var mouseInSub = false;

            sub.on('mouseenter', function (e) {
                mouseInSub = true;
            }).on('mouseleave', function (e) {
                mouseInSub = false;
            })

            var mouseTrack = [];

            var moveHandler = function (e) {
                mouseTrack.push({ //获取鼠标位置
                    x: e.pageX,
                    y: e.pageY
                })

                if (mouseTrack.length > 3) {  //保留三个鼠标位置
                    mouseTrack.shift();
                }
            }

            function vector(a, b) {  //定义向量
                return {
                    x: b.x - a.x,
                    y: b.y - a.y
                }
            }

            function vectorproduct(v1, v2) {  //定义向量叉乘公式
                return v1.x * v2.y - v2.x * v1.y;
            }

            function isPointInTrangle(p, a, b, c) {
                var ab = vector(a, b);
                var ap = vector(a, p);
                var bc = vector(b, c);
                var bp = vector(b, p);
                var ca = vector(c, a);
                var cp = vector(c, p);

                var t1 = vectorproduct(ab, ap);
                var t2 = vectorproduct(bc, bp);
                var t3 = vectorproduct(ca, cp);

                return panduan(t1, t2, t3);
            }

            function panduan(a, b, c) { //向量叉乘算法 大于0点在向量左边，三个都大于0则断定 点在三角形内
                if (a >= 0 && b >= 0 && c >= 0) {
                    return true;
                }
                else
                    return false;
            }

            function needDelay(elem, leftCorner, currMousePos) { //判断是否需要延迟
                var offset = elem.offset();

                var topleft = {         //上边缘坐标
                    x: offset.left,
                    y: offset.top
                }

                var bottomleft = {       //下边缘坐标
                    x: offset.left,
                    y: offset.top + elem.height()
                }

                return isPointInTrangle(currMousePos, leftCorner, topleft, bottomleft); //判断点是否在三角形内
            }


            $("#site-test").on('mouseenter', function (e) {

                $(document).bind('mousemove', moveHandler); //给document绑定鼠标移动事件

            })

            $("#site-test").on('mouseleave', function (e) {

                $('#' + activeRow.data('id') + '').addClass('none'); //隐藏二级菜单的内容
                $('#sub').addClass('none');//隐藏二级菜单

                if (activeRow) // 去掉上一个一级菜单背景色
                {
                    activeRow.removeClass('active');
                    activeRow = null;
                }

                $(document).unbind('mousemove', moveHandler); //给document解除鼠标移动事件
            })



            $(".site-category-ul").on('mouseenter', 'li', function (e) {
                sub.removeClass('none');
                if (!activeRow) {

                    $("#j_navMenu").stop().slideUp(100);
                    activeRow = $(e.target).addClass('active');
                    activeMenu = $('#' + activeRow.data('id'));
                    activeMenu.removeClass('none');

                    return;
                }

                if (timer) {

                    clearTimeout(timer);
                }

                var currMousePos = mouseTrack[mouseTrack.length - 1]; //鼠标当前坐标

                var leftCorner = mouseTrack[mouseTrack.length - 2];  //a点坐标

                var delay = needDelay(sub, leftCorner, currMousePos); //上下边缘坐标

                if (delay) { //在三角形内，setimeout延迟特性加进来

                    timer = setTimeout(function () {
                        if (mouseInSub) {
                            return;
                        }

                        activeRow.removeClass('active');
                        activeMenu.addClass('none');

                        activeRow = $(e.target);

                        activeRow.addClass('active');
                        activeMenu = $('#' + activeRow.data('id'));
                        activeMenu.removeClass('none');
                        timer = null;

                    }, 300)
                }
                else {

                    var prevActiveRow = activeRow;
                    var prevActiveMenu = activeMenu;

                    activeRow = $(this);

                    activeMenu = $('#' + activeRow.data('id'));

                    prevActiveRow.removeClass('active');
                    prevActiveMenu.addClass('none');

                    activeRow.addClass('active');
                    activeMenu.removeClass('none');
                }


            })

            //轮播图

            var index = 1;
            var animated = false;

            $('#home-hero-next').click(function () { //左按钮
                if (!animated) {
                    var usedname = '.img-' + index;
                    if (index == 5) {
                        index = 1;
                    }
                    else {
                        index += 1;
                    }

                    var name = '.img-' + index;

                    animate(usedname, name, 1, 0)
                    showbutton()
                }

            })

            $('#home-hero-prev').click(function () { //右按钮
                if (!animated) {
                    var usedname = '.img-' + index;
                    if (index == 1) {
                        index = 5;
                    }
                    else {
                        index -= 1;
                    }

                    var name = '.img-' + index;

                    animate(name, usedname, 0, 1)
                    showbutton()
                }
            })

            function animate(usedname, name, i, j) {  //左右按钮
                if (timer) {
                    clearTimeout(timer); //去抖技术，在事件被频繁出发时，只执行一次处理
                }
                animated = true;
                $('' + name + '').css("opacity", "" + i + "");
                $('' + usedname + '').css("opacity", "" + j + "");
                timer = setTimeout(function () {  //左右按钮加入延迟，防止快速切换
                    animated = false;
                }, 500);
            }

            function showbutton() {    //点击左右按钮时控制圆点按钮
                $("#home-hero-buttons span:nth-child(" + index + ")").addClass('on').siblings().removeClass('on');

                //方法2    $("#buttons span").eq(index - 1).addClass('on').siblings().removeClass('on');

            }

            $("#home-hero-buttons").on('click', 'span', function (e) {
                var a = $(e.target).index() + 1;

                var usedname = '.img-' + index;

                var name = '.img-' + a;
                index = a;


                $("#home-hero-buttons span:nth-child(" + index + ")").addClass('on').siblings().removeClass('on');
                $('' + usedname + '').css("opacity", "0");
                $('' + name + '').css("opacity", "1");
            })


            $("#home-hero-slider").mouseover(function () {  //自动播放

                clearInterval(timer);
            })
            $("#home-hero-slider").mouseout(function () {

                play();
            })


            play();

            function play() {
                timer = setInterval(function () {

                    $("#home-hero-next").click();

                }, 4000);
            }
        })
